package com.yuda.tools.authreport.controller;

import cn.hutool.core.io.FileUtil;
import cn.hutool.extra.ssh.JschUtil;
import cn.hutool.extra.ssh.Sftp;
import cn.hutool.poi.excel.ExcelReader;
import cn.hutool.poi.excel.ExcelUtil;
import com.jcraft.jsch.Session;
import com.yuda.tools.authreport.service.StudentInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

@RestController
public class FindPdfNotExits {

    @Autowired
    private StudentInfoService studentInfoServiceImpl;

    public String existsHeadPortrait(String idCard) {
        return studentInfoServiceImpl.existsHeadPortrait(idCard);
    }


    @GetMapping("/searchPdfNotExits")
    public void search() {
        ExcelReader reader = ExcelUtil.getReader(FileUtil.file("C:\\Users\\AI_Win10\\Desktop\\A.xlsx"));
        List<List<Object>> read = reader.read(1);
        System.out.println(read.size());

//        String[] idCardss = idCards.split("\n");
//        String[] namess = names.split("\n");
//        String[] provincess = provinceStr.split("\n");
        Session session = JschUtil.getSession("182.92.96.88", 22, "root", "Thussat@200518");
        Sftp sftp = JschUtil.createSftp(session);
        System.out.println("session.isConnected() ===  >  " + session.isConnected());
//        sftp.cd("/mnt/auth_pdf/download/202198959");
        List<String> list = sftp.lsFiles("/mnt/auth_pdf/download/20211121515");

        Set<String> collect = new HashSet<>(list);
        System.out.println(collect.size());
        HashSet<String> strings = new HashSet<>();
        for (int i = 0; i < read.size(); i++) {
            List<Object> objects = read.get(i);
            String idCard = (String) objects.get(13);
            String examUuid = (String) objects.get(14);
            String provinceName = (String) objects.get(11);
            String name = (String) objects.get(12);
            //根据身份证查询 头像是否正常
            String s = existsHeadPortrait(idCard);
            if (s == null || s.length() < 62) {
                System.out.println("头像有问题..." + s);
            } else {
                System.out.println("正常...");
            }

            if (examUuid.startsWith("TACA")) {
                if (strings.contains(examUuid + provinceName + name + idCard.substring(idCard.length() - 6) + ".pdf")) {
                    System.out.println("重复....." + examUuid + provinceName + name + idCard.substring(idCard.length() - 6) + ".pdf");
                } else {
                    strings.add(idCard + examUuid + provinceName + name);
                }

                if (!collect.contains(examUuid + provinceName + name + idCard.substring(idCard.length() - 6) + ".pdf")) {
                    System.out.println(examUuid + provinceName + name + idCard.substring(idCard.length() - 6) + ".pdf");
                }
            } else {
                if (strings.contains("TDA" + examUuid.substring(7, 15) + provinceName + name + idCard.substring(idCard.length() - 6) + ".pdf")) {
                    System.out.println("重复....." + "TDA" + examUuid.substring(7, 15) + provinceName + name + idCard.substring(idCard.length() - 6) + ".pdf");
                } else {
                    strings.add("TDA" + examUuid.substring(7, 15) + provinceName + name + idCard.substring(idCard.length() - 6) + ".pdf");
                }

                if (!collect.contains("TDA" + examUuid.substring(7, 15) + provinceName + name + idCard.substring(idCard.length() - 6) + ".pdf")) {
                    System.out.println("TDA" + examUuid.substring(7, 15) + provinceName + name + idCard.substring(idCard.length() - 6) + ".pdf");
                }
            }

        }
        sftp.close();
        session.disconnect();
    }

}
